#!/usr/bin/python

from FlatSpeedCurve import *
from LinearDemandCurve import *
from StirlingModel import *
from pylab import *

# This can search as an example for a more robust and general method later.

A = 1.5e-4;
B = 2.0
C = 1.0
E = 1.0
VdotO2min = 10.0
VdotO2max = 50.0

ds = [ 200.0, 200.0, 200.0, 200.0 ]
t = arange( 0.0, 2.0+0.01, 0.01 )

demand = LinearDemandCurve( 10.0, 50.0, 300.0 )

split_min = float(0.43)
split_max = float(0.53)
final_time = float(2.0)
detail = 10;

# For a given time in a run, find the splits that use the least anaerobic energy.
ts_best = [ 0.0, 0.0, 0.0, 0.0 ]
VO2anaerobic_best = float(1000.0)
count = 0

# This method performs an exhaustive search over the area.
for ts1 in linspace( split_min, split_max, num=detail ):
	for ts2 in linspace( split_min, split_max, num=detail ):
		for ts3 in linspace( split_min, split_max, num=detail ):
			ts4 = final_time - ts1 - ts2 -ts3
			if ( ( ts4 > split_min ) and ( ts4 < split_max ) ):
				ts = [ ts1, ts2, ts3, ts4 ]
				speed = FlatSpeedCurve( ts, ds, t )
				v = speed.getSpeed()
				D = demand.getDemand( v )

				model = StirlingModel( A, B, C, E, VdotO2min, VdotO2max )
				model.solve( t, D, 10.0*8.0/6.0 )
				VO2anaerobic = model.getAnaerobicTotal()

				count += 1
				print( "%d - [ %g, %g, %g, %g ]: %g" % ( count, ts1, ts2, ts3, ts4, VO2anaerobic ) )

				if ( VO2anaerobic < VO2anaerobic_best ):
					ts_best = ts
					VO2anaerobic_best = VO2anaerobic

print( "Best - [ %g, %g, %g, %g ]: %g" % ( ts_best[0], ts_best[1], ts_best[2], ts_best[3], VO2anaerobic_best ) )
